#include once "fbgfx.bi"
#include once "chipmunk.bi"
#include once "math/vecf.bi"
#include once "gfx.bi"


declare function main( byval argc as integer, byval arv as zstring ptr ptr ) as integer


end main( __FB_ARGC__, __FB_ARGV__ )


function main( byval argc as integer, byval arv as zstring ptr ptr ) as integer

    screenres 640,480,32,,FB.GFX_HIGH_PRIORITY

    'setup a bunch of crap for testing
    cpInitChipmunk()
    dim as cpSpace ptr World = cpSpaceNew()
    World->gravity = type( 0, 50d )

    dim as cpBody ptr tBody = cpBodyNew( 50, .5 )
    cpSpaceAddBody( World, tBody)

    dim as cpShape ptr tShape = cpCircleShapeNew(tBody, 128d, cpV(0,0) )
    cpSpaceAddShape( World, tShape)

    tBody->t = -50d
    tBody->v = cpV( 50, 0 )

    dim as FB.IMAGE ptr testi = imagecreate( 128, 128 )
    bload "res/gfx/test_sprite.bmp", testi


    dim as double frame_rate = 60
    dim as double frequency = 1.0/frame_rate
    dim as double ltime, ttime, accum_step, tstep, fps_timer
    dim as integer frames_ps
    dim as string fps_string


    'our test loop...
    ltime = timer
    do

        'test the simulation loop and timing
        tstep = timer - ltime
        accum_step += tstep
        do

            cpSpaceStep( World, frequency )

            cpBodyResetForces( tBody )

            accum_step -= frequency

        loop while accum_step > frequency

        ltime = timer

        'fps counter...
        frames_ps+=1
        if ltime>fps_timer then
            fps_string = "FPS " & frames_ps
            fps_timer = ltime + 1
            frames_ps = 0
        end if


        'test render...
        screenlock
        cls

        rotozoom( 0, testi, tBody->p.x, tBody->p.y, tBody->a,1,1 )

        locate 1,1
        print fps_string

        screensync
        screenunlock

        sleep 1,1

    loop until multikey(FB.SC_ESCAPE)


    imagedestroy(testi)
    cpSpaceFree(World)

    return 0

end function